package timing.ukulele.form.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import timing.ukulele.form.persistent.EntityFieldConfig;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.form.mapper.EntityFieldConfigMapper;
import timing.ukulele.persistent.service.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class EntityFieldConfigService extends BaseService<EntityFieldConfigMapper, EntityFieldConfig> {

    /**
     * 分页获取字段属性
     *
     * @param configId 配置表id
     * @param description           描述
     * @param field           字段
     * @param pageNum         当前页
     * @param pageSize        每页大小
     */
    public ResponseResult<IPage<EntityFieldConfig>> page(Long configId, String description, String field, Integer pageNum, Integer pageSize) {
        Page<EntityFieldConfig> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<EntityFieldConfig> query = new LambdaQueryWrapper<>();
        query.eq(EntityFieldConfig::getEntityConfigId, configId);
        if (!StringUtils.isEmpty(description)) {
            query.like(EntityFieldConfig::getDescription, description);
        }
        if (!StringUtils.isEmpty(field)) {
            query.like(EntityFieldConfig::getField, field);
        }
        query.orderByDesc(EntityFieldConfig::getUpdateTime);
        IPage<EntityFieldConfig> iPage = getBaseMapper().selectPage(page, query);
        return ResponseResult.success(iPage);
    }
}
